package com.yeyaomai.dksns.flowis.service.result;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 关键字获取以及搜索引擎确定类
 * 
 * @author xiaqin
 * @date 2012-05-22
 */
public class KeyWordGain {
	/*
	 * 匹配正则表达式，获取关键字，并且解码
	 */
	public static String getKeyword(String url) {
		String keywordReg = "(?:yahoo.+?[\\?|&]p=|openfind.+?query=|google.+?q=|lycos.+?query=|onseek.+?keyword=|search\\.tom.+?word=|search\\.qq\\.com.+?word=|zhongsou\\.com.+?word=|search\\.msn\\.com.+?q=|yisou\\.com.+?p=|sina.+?word=|video.baidu.+?word=|zhidao.baidu.+?word=|image.baidu.+?word=|tieba.baidu.+?kw=|sina.+?query=|sina.+?_searchkey=|sohu.+?word=|sohu.+?key_word=|sohu.+?query=|163.+?q=|baidu.+?wd=|sogou.+?query=|yahoo.+?q=|youdao.+?q=|yisou.+?q=|search.sina.+?q=|cn.bing.+?q=|soso.+?w=|3721\\.com.+?p=|Alltheweb.+?q=)([^&]*)";
		String encodeReg = "^(?:[\\x00-\\x7f]|[\\xfc-\\xff][\\x80-\\xbf]{5}|[\\xf8-\\xfb][\\x80-\\xbf]{4}|[\\xf0-\\xf7][\\x80-\\xbf]{3}|[\\xe0-\\xef][\\x80-\\xbf]{2}|[\\xc0-\\xdf][\\x80-\\xbf])+$";
		Pattern keywordPatt = Pattern.compile(keywordReg);
		StringBuffer keyword = new StringBuffer(20);
		Matcher keywordMat = keywordPatt.matcher(url);
		while (keywordMat.find()) {
			keywordMat.appendReplacement(keyword, "$1");
		}
		if (!keyword.toString().equals("")) {
			String keywordsTmp = keyword.toString().replace("http://www.", "");
			keywordsTmp = keywordsTmp.replace("http://", "");
			Pattern encodePatt = Pattern.compile(encodeReg);
			String unescapeString = KeyWordGain.unescape(keywordsTmp);
			Matcher encodeMat = encodePatt.matcher(unescapeString);
			String encodeString = "gbk";
			if (encodeMat.matches())
				encodeString = "utf-8";
			try {
				return URLDecoder.decode(keywordsTmp, encodeString);
			} catch (UnsupportedEncodingException e) {
				return "";
			}
		}
		return "";
	}

	/*
	 * 用于对搜索关键字的解码工作
	 */
	private static String unescape(String src) {
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(src.length());
		int lastPos = 0, pos = 0;
		char ch;
		while (lastPos < src.length()) {
			pos = src.indexOf("%", lastPos);
			if (pos == lastPos) {
				if (src.charAt(pos + 1) == 'u') {
					ch = (char) Integer.parseInt(
							src.substring(pos + 2, pos + 6), 16);
					tmp.append(ch);
					lastPos = pos + 6;
				} else {
					ch = (char) Integer.parseInt(
							src.substring(pos + 1, pos + 3), 16);
					tmp.append(ch);
					lastPos = pos + 3;
				}
			} else {
				if (pos == -1) {
					tmp.append(src.substring(lastPos));
					lastPos = src.length();
				} else {
					tmp.append(src.substring(lastPos, pos));
					lastPos = pos;
				}
			}
		}
		return tmp.toString();
	}

	/*
	 * 获取搜索引擎的中文名称
	 */
	public static String getDomain(String url) {
		String engine = "null";
		if (url.indexOf("baidu") != -1) {
			engine = "百度";
		}
		if (url.indexOf("google") != -1) {
			engine = "谷歌";
		}
		if (url.indexOf("bing") != -1) {
			engine = "微软必应";
		}
		if (url.indexOf("yahoo") != -1) {
			engine = "雅虎";
		}
		if (url.indexOf("sogou") != -1) {
			engine = "搜狗";
		}
		if (url.indexOf("soso") != -1) {
			engine = "腾讯搜搜";
		}
		return engine;
	}
	
}
